<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="https://unpkg.com/layui@2.6.8/dist/css/layui.css">
    <!-- 引入 layui.js -->
    <script src="https://unpkg.com/layui@2.6.8/dist/layui.js"></script>
    <link rel="stylesheet" href="./style.css">
</head>

<body>
    <form class="layui-form loginForm" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" name="username" required lay-verify="required" placeholder="请输入账号" autocomplete="off"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                    autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="loginForm">立即提交</button>
            </div>
        </div>
    </form>


    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.3/jquery.js"></script>
    <script>


        //如果有值，就直接判断是否账号存在
        layui.use('form', function () {
            let form = layui.form;
            //监听提交,获取数据
            form.on('submit(loginForm)', function (data) {
                let users = JSON.parse(localStorage.getItem('users'));
                if (users) {
                    let obj = data.field;//表单数据
                    let result = users.some((item) => item.username == obj.username && item.password == obj.password)
                    if (result) {
                        alert('登陆成功');
                    } else {
                        alert('账号或密码错误')
                    }

                } else {
                    alert('去注册！！！')
                    location.href = 'register.html'
                }

                return false;
            });
        });



    </script>

</body>

</html>